package de.homelabs.moonrat.config;

import java.util.Iterator;

import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.SystemConfiguration;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

@Service
public class MoonratConfigurationManager {

	/****************
	 * vars			*
	 ****************/
	//log4j
	Logger logger = Logger.getLogger(MoonratConfigurationManager.class);
	
	//main composite configuration
	CompositeConfiguration compositeConfiguration = new CompositeConfiguration();
	
	//main config class
	MoonratConfiguration config = new MoonratConfiguration();
	
	//main Flag, if false we cannot do anything
	Boolean properlyConfigured = false;
	
	boolean debug = false;
	
	/**
	 * constructor
	 */
	public MoonratConfigurationManager(){}

	/********************
	 * Methods			*
	 ********************/
	
	/**
	 * initialize the default configuration 
	 * @param moonratStartupConfig - base config from JNDI e.g. fileroot 
	 */
	public void initConfig(MoonratStartupConfig moonratStartupConfig){
		//debug JNDI-Properties
		if (logger.isDebugEnabled()) MoonratJndiPrinter.printJndiProperties(Level.DEBUG);
		
		try {
			//add a new SystemConfigration
			compositeConfiguration.addConfiguration(new SystemConfiguration());
			//add a file properties configuration
			//located in config path read from moonratstartupconfig
			compositeConfiguration.addConfiguration(new PropertiesConfiguration(moonratStartupConfig.getConfigPath() + "/" + "base.properties"));
			
			//parse properties to MoonratConfiguration
			parseMainConfiguration(moonratStartupConfig);
			
			//everything is fine :-)
			properlyConfigured = true;
			logger.info("configuration initialized");
			
			//some debugging
			if (logger.isDebugEnabled()) debugging(moonratStartupConfig);
			
		} catch (ConfigurationException e){
			logger.error("Cannot read/parse configuration settings file '"+moonratStartupConfig.getConfigPath() + "/base.properties' due to configuration exception: "+e.getMessage());
		}
	
	}

	/**
	 * parses the main configuration file
	 * @param moonratStartupConfig
	 * @throws ConfigurationException
	 */
	private void parseMainConfiguration(MoonratStartupConfig moonratStartupConfig) throws ConfigurationException {
		//debug flag
		debug = compositeConfiguration.getBoolean("moonrat.debug");
		
		//base properties
		config.setRootPath(moonratStartupConfig.getConfigPath());
		config.setSupportedDomains(compositeConfiguration.getStringArray("moonrat.domains"));
		
		//dataproviders
		config.setDataProvider(compositeConfiguration.getStringArray("moonrat.dataprovider"));
	}
	
	/**
	 * some debugging
	 */
	private void debugging(MoonratStartupConfig moonratStartupConfig){
		String key;
		
		logger.debug("Debugging start");
		logger.debug("MainProperties: "+moonratStartupConfig.getConfigPath() + "/" + "base.properties");
		logger.debug("Content");
		Iterator<String> keys = compositeConfiguration.getKeys("moonrat");
		
		while (keys.hasNext()){
			key = keys.next();
			logger.debug(key+": "+compositeConfiguration.getProperty(key));
		}
		logger.debug("Debugging finished");
	}
	
	/********************
	 * getter / setter	*
	 ********************/
	public MoonratConfiguration getConfig(){
		return config;
	}

	public boolean isDebug() {
		return debug;
	}
}
